package il.ac.braude.sona.gui.forms.questions;

import java.awt.Color;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * Class for inserting the JText Area into our questions system
 * @author Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 */
@SuppressWarnings("serial")
public class QTextArea extends FormQuestion {
   /**
    * parameter to use as text area
    */
	private JTextArea tf = new JTextArea(3,30);
	private JLabel q;
    /**
     * initialize a new text area for a form
     * sets the component name with the given string
     * @param question - name for the component
     */
	public QTextArea(String question) {
		super(BoxLayout.Y_AXIS);
		init(question);

	}
/**
 * initialize a new label for the text area
 * adds scroll pane
 * @param question - name for the label
 */
	private void init(String question) {
		q = new JLabel(question);
		add(q);
		add(new JScrollPane(tf));
	}
/**
 * initialize new text area with the given name
 * uses the boolean parameter to determine if button fill is mandatory
 * @param question  - name for the text area
 * @param necessity - boolean value
 */
	public QTextArea(String question, boolean necessity) {
		super(BoxLayout.Y_AXIS, necessity);
		init(question);
	}
/**
 * sets editable mode for the component
 * @param isEditable - boolean value to use for setting the editable mode
 */
	public void setEditable(boolean isEditable) {
		tf.setEditable(isEditable);
	}
	/**
	 * sets text area size
	 * @param rows
	 * @param columns
	 */
	public void setAreaSize(int rows,int columns) {
		tf.setRows(rows);
		tf.setColumns(columns);
	}

	@Override
	public void setDefaultAnswer(Object defaultAnswer) {
		tf.setText(defaultAnswer.toString());
	}

	@Override
	public Object getAnswer() {
		String answer = tf.getText();
		return (isNecessary() && answer.length()<=0) ? null : answer;
	}
	@Override
	public void markQuestion() {
		q.setForeground(Color.red);
		this.requestFocus();
	}
	@Override
	public void unmarkQuestion() {
		q.setForeground(Color.black);
	}
}
